package com.taxinfo.service.impl.data;

import com.taxinfo.common.ClientSession;
import com.taxinfo.pojo.dto.AccBalMapInfoDto;
import com.taxinfo.pojo.dto.AssetAndLibDto;
import com.taxinfo.pojo.dto.IncomeStmInfoDto;
import com.taxinfo.pojo.dto.LoginDto;
import com.taxinfo.service.impl.MainReportServiceImpl;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 财务报表2013小企业会计准则.xlsx(月度）
 * @author wanghy
 */
public class MonthAccStandards2013SmallEnterprise extends CommFunction implements MainReportServiceImpl.ExcelUpdateInterface {
    /**
     *
     * @param wb workbook
     * @param accBalMapInfos 映射的数据集合
     * @param type 类型 :年度、季度、月度
     */
    @Override
    public String businessLogic(XSSFWorkbook wb, Map<String,List> dataInfos,String sblx, String sbYear) {

        LoginDto loginDto =(LoginDto) ClientSession.getAttribute("user");
        //所属期处理
        String[] ssq= super.getSsqInfo(sblx,sbYear);
        //填充第一页数据
        Sheet sheet = wb.getSheetAt(0);
        sheet.getRow(2).getCell(2).setCellValue(getString(loginDto.getCreditCode(),"0"));
        sheet.getRow(2).getCell(6).setCellValue(getString(loginDto.getCompanyName(),"0"));
        sheet.getRow(3).getCell(2).setCellValue(getString(ssq[0],"0"));
        sheet.getRow(3).getCell(6).setCellValue(getString(ssq[1],"0"));
        List<AssetAndLibDto> assetMapInfos =  dataInfos.get("assetMapInfos");
        Map<String, AssetAndLibDto> assetAndLibMap = assetMapInfos.stream().collect(Collectors.toMap(AssetAndLibDto::getSubName, assetAndLibDto -> assetAndLibDto));
        this.fillOneSheet(assetAndLibMap,sheet);

        //填充第二sheet页数据
        Sheet stProfit = wb.getSheetAt(1);
        stProfit.getRow(2).getCell(2).setCellValue(getString(loginDto.getCreditCode(),"0"));
        stProfit.getRow(2).getCell(4).setCellValue(getString(loginDto.getCompanyName(),"0"));
        stProfit.getRow(3).getCell(2).setCellValue(getString(ssq[0],"0"));
        stProfit.getRow(3).getCell(4).setCellValue(getString(ssq[1],"0"));

        this.fillSecondSheet(dataInfos,stProfit);

        return "";
    }

    /**
     * 利润表
     * @param dataInfos
     * @param stProfit
     */
    private void fillSecondSheet(Map<String, List> dataInfos, Sheet sheet) {
        List<IncomeStmInfoDto>  incomMapInfos = dataInfos.get("incomMapInfos");
        List<IncomeStmInfoDto> lastIncomeDataInfo = dataInfos.get("lastIncomeDataInfo");
        Map<String, IncomeStmInfoDto> incomMap = incomMapInfos.stream().collect(Collectors.toMap(IncomeStmInfoDto::getSubName, incomeStmInfoDto -> incomeStmInfoDto));
        Map<String, IncomeStmInfoDto> lastIncomMap = lastIncomeDataInfo.stream().collect(Collectors.toMap(IncomeStmInfoDto::getSubName, incomeStmInfoDto -> incomeStmInfoDto));

        int rowCount = sheet.getLastRowNum();
        for (int j = 5; j <= rowCount; j++) {
            Row row = sheet.getRow(j);
            String subName = super.getCellInfo(row.getCell(0)).replaceAll("\\s","").replaceAll("　", "");
            IncomeStmInfoDto incomeDto1 = incomMap.get(subName);
            IncomeStmInfoDto lastIncomDto1 = lastIncomMap.get(subName);
            if(null != incomeDto1){
                row.getCell(2).setCellValue(getAmtDecimal(incomeDto1.getCurrentAmt()).doubleValue());
            }
            if(null != lastIncomDto1){
                row.getCell(3).setCellValue(getAmtDecimal(lastIncomDto1.getCurrentAmt()).doubleValue());
            }
        }
        //其中：利息费用（收入以”-“填列）
        IncomeStmInfoDto dataDto1 = incomMap.get("其中：利息费用");
        if(null != dataDto1){
            sheet.getRow(23).getCell(2).setCellValue(getAmtDecimal(dataDto1.getCurrentAmt()).doubleValue());
        }
        //其中：利息费用（收入以”-“填列）
        IncomeStmInfoDto dataDto2 = lastIncomMap.get("其中：利息费用");
        if(null != dataDto2){
            sheet.getRow(23).getCell(3).setCellValue(getAmtDecimal(dataDto2.getCurrentAmt()).doubleValue());
        }
    }

    /**
     * 资产负债表
     * @param assetAndLibMap
     */
    private void fillOneSheet(Map<String, AssetAndLibDto> assetAndLibMap, Sheet sheet) {
        int rowCount = sheet.getLastRowNum();
        for (int j = 6; j <= rowCount; j++) {
            Row row = sheet.getRow(j);
            String subName = super.getCellInfo(row.getCell(0)).replaceAll("\\s","").replaceAll("　", "");
            AssetAndLibDto assetAndLibDto1 = assetAndLibMap.get(subName);
            if(null != assetAndLibDto1){
                row.getCell(2).setCellValue(getAmtDecimal(assetAndLibDto1.getEndBal()).doubleValue());
                row.getCell(3).setCellValue(getAmtDecimal(assetAndLibDto1.getOpenBal()).doubleValue());
            }
            subName = super.getCellInfo(row.getCell(4)).replaceAll("\\s","").replaceAll("　", "");
            AssetAndLibDto assetAndLibDto2 = assetAndLibMap.get(subName);
            if(null != assetAndLibDto2){
                row.getCell(6).setCellValue(getAmtDecimal(assetAndLibDto2.getEndBal()).doubleValue());
                row.getCell(7).setCellValue(getAmtDecimal(assetAndLibDto2.getOpenBal()).doubleValue());
            }
        }
        //减：未处理资产损失
        AssetAndLibDto dataDto = assetAndLibMap.get("减：未处理资产损失");
        if(null != dataDto){
            sheet.getRow(34).getCell(6).setCellValue(getAmtDecimal(dataDto.getEndBal()).doubleValue());
            sheet.getRow(34).getCell(7).setCellValue(getAmtDecimal(dataDto.getOpenBal()).doubleValue());
        }
    }
}